<?php
/**
 * Load WordPress core functions
 */
if (!function_exists('wp_enqueue_script')) {
    require_once(ABSPATH . 'wp-load.php');
    require_once(ABSPATH . 'wp-includes/pluggable.php');
}

function zb_assets_src($cdn_mod='theme'){
    //font-awesome图标/jquery加载模式
    $assets_mod = [
        'theme'      => [
            'jquery'       => get_template_directory_uri() . '/assets/js/jquery.min.js',
            'highlight-js' => get_template_directory_uri() . '/assets/js/highlight.min.js',
            'video-js'     => get_template_directory_uri() . '/assets/js/video-js/video.min.js',
            'video-css'    => get_template_directory_uri() . '/assets/css/video-js/video-js.min.css',
            'fw'           => get_template_directory_uri() . '/assets/css/font-awesome/css/all.min.css',
            'fw4'          => get_template_directory_uri() . '/assets/css/font-awesome/css/v4-shims.min.css',
        ],
        'jsdelivr'   => [
            'jquery'       => '//cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js',
            'highlight-js' => '//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/highlight.min.js',
            'video-js'     => '//cdn.jsdelivr.net/npm/video.js@8.0.4/dist/video.min.js',
            'video-css'    => '//cdn.jsdelivr.net/npm/video.js@8.0.4/dist/video-js.min.css',
            'fw'           => '//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.0/css/all.min.css',
            'fw4'          => '//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.0/css/v4-shims.min.css',
        ],
        'unpkg'      => [
            'jquery'       => '//unpkg.com/jquery@3.6.4/dist/jquery.min.js',
            'highlight-js' => '//unpkg.com/@highlightjs/cdn-assets@11.7.0/highlight.min.js',
            'video-js'     => '//unpkg.com/video.js/dist/video.min.js',
            'video-css'    => '//unpkg.com/video.js@8.0.4/dist/video-js.min.css',
            'fw'           => '//unpkg.com/@fortawesome/fontawesome-free@6.4.0/css/all.min.css',
            'fw4'          => '//unpkg.com/@fortawesome/fontawesome-free@6.4.0/css/v4-shims.min.css',
        ],

        'cloudflare' => [
            'jquery'       => '//cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
            'highlight-js' => '//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js',
            'video-js'     => '//cdnjs.cloudflare.com/ajax/libs/video.js/8.0.4/video.min.js',
            'video-css'    => '//cdnjs.cloudflare.com/ajax/libs/video.js/8.0.4/video-js.min.css',
            'fw'           => '//cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css',
            'fw4'          => '//cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/v4-shims.min.css',
        ],
        'bootcdn'    => [
            'jquery'       => '//cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js',
            'highlight-js' => '//cdn.bootcdn.net/ajax/libs/highlight.js/11.7.0/highlight.min.js',
            'video-js'     => '//cdn.bootcdn.net/ajax/libs/video.js/8.0.4/video.min.js',
            'video-css'    => '//cdn.bootcdn.net/ajax/libs/video.js/8.0.4/video-js.min.css',
            'fw'           => '//cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css',
            'fw4'          => '//cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/v4-shims.min.css',
        ],
    ];

    return $assets_mod[$cdn_mod];
}

function style_file($mod){
    $assets=  [
        'plugin'            =>get_template_directory_uri(). '/assets/css/plugin.css',
        'collection'        =>get_template_directory_uri(). '/assets/css/collection2.css',
        'feather'           =>get_template_directory_uri(). '/assets/css/feather.css',
        'blog'              =>get_template_directory_uri(). '/assets/css/blog.css',
        'other-pages'       =>get_template_directory_uri(). '/assets/css/other-pages.css',
        'product-page'      =>get_template_directory_uri(). '/assets/css/product-page.css',
        'style1'              =>get_template_directory_uri(). '/assets/css/style2.css',
//        'blog'              => get_template_directory_uri() . '/assets/js/blog.js',
//        'magnific-popup'    => get_template_directory_uri() . '/assets/js/magnific-popup.min.js',
//        'main'              => get_template_directory_uri() . '/assets/js/main.js',
//        'phosphor-icons'    => get_template_directory_uri() . '/assets/js/phosphor-icons.js',
//        'product-detail'    => get_template_directory_uri() . '/assets/js/product-detail.js',
//        'slick'             => get_template_directory_uri() . '/assets/js/slick.min.js',
//        'swiper-bundle-js'  => get_template_directory_uri() . '/assets/js/swiper-bundle.min.js',
//        'shop'              => get_template_directory_uri() . '/assets/js/shop.js',
//        'swiper-bundle-css' => get_template_directory_uri() . '/assets/css/swiper-bundle.min.css',
//        'style'             => get_template_directory_uri() . '/assets/css/style.css',
//        'output-scss'       => get_template_directory_uri() . '/assets/css/output-scss.css',
//         'output-tailwind'  => get_template_directory_uri() . '/assets/css/output-tailwind.css'

    ];
    return $assets[$mod];
}

/**
 * 智能资源加载系统 - 条件式CSS/JS加载
 * Smart Asset Loading System - Conditional CSS/JS Loading
 */
function wbw_smart_asset_loading() {
    // 清理不需要的WordPress默认样式
    wbw_cleanup_default_styles();
    
    // 获取当前页面类型
    $page_type = wbw_get_current_page_type();
    
    // 设置版本号和后缀
    $assets_ver = (_THEME_DEBUG == true) ? _THEME_VERSION . time() : _THEME_VERSION;
    $js_suffix = (_THEME_DEBUG == true) ? '.js' : '.min.js';
    $css_suffix = (_THEME_DEBUG == true) ? '.css' : '.min.css';
    
    // 获取CDN设置
    $cdn_mod = _xin('assets_cdn_mod', 'theme');
    $cdn_src = zb_assets_src($cdn_mod);
    
    // 加载核心资源（所有页面都需要）
    wbw_load_core_assets($assets_ver, $cdn_src);
    
    // 根据页面类型条件加载特定资源
    wbw_load_conditional_assets($page_type, $assets_ver);
    
    // 加载WooCommerce相关资源
    if (class_exists('WooCommerce')) {
        wbw_load_woocommerce_assets($page_type, $assets_ver);
    }
    
    // 加载性能优化资源
    wbw_load_performance_assets($assets_ver);
}
add_action('wp_enqueue_scripts', 'wbw_smart_asset_loading');

/**
 * 清理WordPress默认样式
 */
function wbw_cleanup_default_styles() {
    // 移除WordPress默认全局样式
    wp_deregister_style('global-styles');
    wp_dequeue_style('global-styles');
    wp_dequeue_style('wp-block-library-theme');
    wp_dequeue_style('wc-block-style');
    wp_dequeue_style('wc-blocks-style');
    
    // 移除全局样式动作
    remove_action('wp_enqueue_scripts', 'wp_enqueue_global_styles');
    remove_action('wp_footer', 'wp_enqueue_global_styles', 1);
    
    // 在非必要页面移除块编辑器样式
    if (!is_admin() && !is_singular('post') && !is_page()) {
        wp_dequeue_style('wp-block-library');
    }
}

/**
 * 获取当前页面类型
 */
function wbw_get_current_page_type() {
    if (is_front_page()) {
        return 'home';
    } elseif (is_shop() || is_product_category() || is_product_tag()) {
        return 'shop';
    } elseif (is_product()) {
        return 'product';
    } elseif (is_cart()) {
        return 'cart';
    } elseif (is_checkout()) {
        return 'checkout';
    } elseif (is_account_page()) {
        return 'account';
    } elseif (is_home() || is_archive() || is_category() || is_tag()) {
        return 'blog';
    } elseif (is_singular('post')) {
        return 'post';
    } elseif (is_page()) {
        return 'page';
    } elseif (is_search()) {
        return 'search';
    } elseif (is_404()) {
        return '404';
    } else {
        return 'other';
    }
}

/**
 * 加载核心资源（所有页面）
 */
function wbw_load_core_assets($assets_ver, $cdn_src) {
    $template_uri = get_template_directory_uri();
    
    // 核心CSS（所有页面都需要）
    wp_enqueue_style('wbw-plugin', $template_uri . '/assets/css/plugin.css', array(), $assets_ver, 'all');
    wp_enqueue_style('wbw-feather', $template_uri . '/assets/css/feather.css', array(), $assets_ver, 'all');
    
    // 核心JavaScript
    wp_enqueue_script('wbw-plugin', $template_uri . '/assets/js/plugin.js', array('jquery'), $assets_ver, true);
    wp_enqueue_script('wbw-main', $template_uri . '/assets/js/main.js', array('jquery'), $assets_ver, true);
    
    // jQuery处理
    if (_xin('use_cdn_jquery', false)) {
        wp_deregister_script('jquery');
        wp_enqueue_script('jquery', $cdn_src['jquery'], array(), '3.6.0', false);
    }
    
    // 添加全局JavaScript变量
    wp_localize_script('wbw-main', 'wbw_ajax', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('wbw_nonce'),
        'site_url' => home_url('/'),
        'theme_url' => $template_uri,
        'is_user_logged_in' => is_user_logged_in(),
        'currency_symbol' => class_exists('WooCommerce') ? get_woocommerce_currency_symbol() : '$'
    ));
}

/**
 * 根据页面类型条件加载资源
 */
function wbw_load_conditional_assets($page_type, $assets_ver) {
    $template_uri = get_template_directory_uri();
    
    switch ($page_type) {
        case 'home':
            wbw_load_homepage_assets($assets_ver);
            break;
            
        case 'shop':
            wp_enqueue_style('wbw-collection', $template_uri . '/assets/css/collection.css', array('wbw-plugin'), $assets_ver, 'all');
            wp_enqueue_script('wbw-shop', $template_uri . '/assets/js/shop.js', array('wbw-main'), $assets_ver, true);
            break;
            
        case 'product':
            wp_enqueue_style('wbw-product', $template_uri . '/assets/css/product-page.css', array('wbw-plugin'), $assets_ver, 'all');
            wp_enqueue_script('wbw-product', $template_uri . '/assets/js/product.js', array('wbw-main'), $assets_ver, true);
            break;
            
        case 'blog':
        case 'post':
            wp_enqueue_style('wbw-blog', $template_uri . '/assets/css/blog.css', array('wbw-plugin'), $assets_ver, 'all');
            // 代码高亮（仅文章页面）
            if ($page_type === 'post') {
                wp_enqueue_script('wbw-highlight', get_template_directory_uri() . '/assets/js/highlight.min.js', array('jquery'), '11.7.0', true);
            }
            break;
            
        case 'page':
        case 'cart':
        case 'checkout':
        case 'account':
        case 'search':
        case '404':
        case 'other':
            wp_enqueue_style('wbw-other', $template_uri . '/assets/css/other-pages.css', array('wbw-plugin'), $assets_ver, 'all');
            break;
    }
}

/**
 * 加载首页资源
 */
function wbw_load_homepage_assets($assets_ver) {
    $template_uri = get_template_directory_uri();
    $homepage_template = get_current_homepage_template();
    
    // 根据首页模板加载对应样式
    switch ($homepage_template) {
        case 'template_2':
            wp_enqueue_style('wbw-home', $template_uri . '/assets/css/style2.css', array('wbw-plugin'), $assets_ver, 'all');
            break;
        case 'template_3':
            wp_enqueue_style('wbw-home', $template_uri . '/assets/css/style3.css', array('wbw-plugin'), $assets_ver, 'all');
            break;
        default:
            wp_enqueue_style('wbw-home', $template_uri . '/assets/css/style1.css', array('wbw-plugin'), $assets_ver, 'all');
    }
    
    // 首页特殊功能
    wp_enqueue_script('wbw-homepage', $template_uri . '/assets/js/homepage.js', array('wbw-main'), $assets_ver, true);
    
    // 邮件订阅功能
    if (_xin('enable_newsletter', true)) {
        wp_enqueue_script('wbw-newsletter', $template_uri . '/assets/js/newsletter.js', array('jquery'), $assets_ver, true);
    }
}

/**
 * 加载WooCommerce相关资源
 */
function wbw_load_woocommerce_assets($page_type, $assets_ver) {
    if (!is_woocommerce() && !is_cart() && !is_checkout() && !is_account_page()) {
        return;
    }
    
    $template_uri = get_template_directory_uri();
    
    // WooCommerce核心样式覆盖
    wp_enqueue_style('wbw-woo-override', $template_uri . '/inc/override-styles.css', array(), $assets_ver, 'all');
    
    // WooCommerce功能脚本
    wp_enqueue_script('wbw-woocommerce', $template_uri . '/assets/js/woocommerce.js', array('wbw-main'), $assets_ver, true);
    
    // 页面特定资源
    switch ($page_type) {
        case 'shop':
            wp_enqueue_script('wbw-shop-filter', $template_uri . '/assets/js/shop-filter.js', array('wbw-woocommerce'), $assets_ver, true);
            break;
            
        case 'product':
            wp_enqueue_script('wbw-product-gallery', $template_uri . '/assets/js/product-gallery.js', array('wbw-woocommerce'), $assets_ver, true);
            wp_enqueue_script('wbw-product-variations', $template_uri . '/assets/js/product-variations.js', array('wbw-woocommerce'), $assets_ver, true);
            break;
            
        case 'cart':
            wp_enqueue_script('wbw-cart', $template_uri . '/assets/js/cart.js', array('wbw-woocommerce'), $assets_ver, true);
            break;
            
        case 'checkout':
            wp_enqueue_script('wbw-checkout', $template_uri . '/assets/js/checkout.js', array('wbw-woocommerce'), $assets_ver, true);
            break;
    }
    
    // Ajax购物车（全局）
    if (_xin('enable_ajax_cart', true)) {
        wp_enqueue_script('wbw-ajax-cart', $template_uri . '/assets/js/ajax-cart.js', array('wbw-woocommerce'), $assets_ver, true);
    }
    
    // 愿望清单功能
    if (_xin('enable_wishlist', true)) {
        wp_enqueue_script('wbw-wishlist', $template_uri . '/assets/js/wishlist.js', array('wbw-woocommerce'), $assets_ver, true);
    }
    
    // 产品比较功能
    if (_xin('enable_product_compare', true)) {
        wp_enqueue_script('wbw-product-compare', $template_uri . '/assets/js/product-compare.js', array('wbw-woocommerce'), $assets_ver, true);
    }
}

/**
 * 加载性能优化资源
 */
function wbw_load_performance_assets($assets_ver) {
    // 懒加载图片
    if (_xin('enable_lazy_loading', true)) {
        wp_enqueue_script('wbw-lazy-load', get_template_directory_uri() . '/assets/js/lazy-load.js', array(), $assets_ver, true);
    }
    
    // 移动端优化
    if (wp_is_mobile()) {
        wp_enqueue_style('wbw-mobile-responsive', get_template_directory_uri() . '/assets/css/mobile-responsive.css', array('wbw-plugin'), $assets_ver, 'all');
        wp_enqueue_script('wbw-mobile-responsive', get_template_directory_uri() . '/assets/js/mobile-responsive.js', array('wbw-main'), $assets_ver, true);
    }
    
    // 评论回复脚本（仅需要时加载）
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
    
    // 视频支持（按需加载）
    if (is_singular()) {
        $post_content = get_the_content();
        if (has_shortcode($post_content, 'video') || 
            get_post_meta(get_the_ID(), 'xin_video', true) ||
            in_array(get_post_format(get_the_ID()), array('video', 'audio'))) {
            
            $cdn_src = zb_assets_src(_xin('assets_cdn_mod', 'theme'));
            wp_enqueue_style('wbw-video', $cdn_src['video-css'], array(), '8.0.4', 'all');
            wp_enqueue_script('wbw-video', $cdn_src['video-js'], array(), '8.0.4', true);
        }
    }
}
// 旧函数保持兼容性
function zb_scripts() {
    wbw_smart_asset_loading();
}
add_action('wp_enqueue_scripts', 'zb_scripts', 5);

// // admin
// function zb_enqueue_admin_script($hook) {

//     wp_enqueue_style('zb-admin-all', get_template_directory_uri() . '/admin/css/admin-all.css', array(), _THEME_VERSION);

//     wp_enqueue_script('zb-admin-all', get_template_directory_uri() . '/admin/js/admin-all.js', array('jquery'), _THEME_VERSION, true);

//     $script_params = array(
//         'home_url'   => esc_url(home_url()),
//         'ajax_url'   => esc_url(admin_url('admin-ajax.php')),
//         'theme_url'  => esc_url(get_template_directory_uri()),
//         'ajax_nonce' => wp_create_nonce("zb_ajax"),
//     );

//     wp_localize_script('zb-admin-all', 'zb', $script_params);

//     //商城管理页面加载
//     if (strpos($hook, 'zb-admin-page') !== false) {
//         wp_enqueue_style('zb-admin-page', get_template_directory_uri() . '/admin/css/admin-page.css', array(), _THEME_VERSION);
//         wp_enqueue_script('apexcharts', get_template_directory_uri() . '/admin/js/apexcharts.min.js', array(), '3.35.3', true);
//         wp_enqueue_script('zb-admin-page', get_template_directory_uri() . '/admin/js/admin.js', array('jquery'), _THEME_VERSION, true);
//     }

// }
// add_action('admin_enqueue_scripts', 'zb_enqueue_admin_script');
